package LC02;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
        next = null;
    }
}

public class LC0141 {
    public static void main(String[] args) {
        ListNode listNode2 = new ListNode(1);
        ListNode listNode1 = new ListNode(2);
        listNode1.next = listNode2;

        System.out.println(hasCycle(listNode1));
    }

    public static boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        while (head != null) {
            if (!set.add(head)) {
                return true;
            }
            head = head.next;
        }
        return false;
    }
}
